package com.yjs.app.trade.web.controller.base;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.yjs.app.trade.service.service.util.FastDFSUtil;
import com.yjs.app.trade.web.controller.base.util.Result;

@Controller
@RequestMapping(value = "/app/file")
public class FileUploadController {
	
	/**
	 * 发送手机验证码
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	@ResponseBody
	public Object sendPhoneCode(MultipartHttpServletRequest request) {
		Iterator<String> itr = request.getFileNames();
		MultipartFile file;
		List<String> list = new ArrayList<String>();
		while (itr.hasNext()) {
			try {
				file = request.getFile(itr.next());
				String fileName = file.getOriginalFilename();
				if (fileName.contains(".png") || fileName.contains(".mp4") || fileName.contains(".jpg") || fileName.contains(".jpeg") || 
						fileName.contains(".bmp") || fileName.contains(".gif") || fileName.contains(".mp3")) {
					String fileUrl = FastDFSUtil.uploadFile(file);
					list.add(fileUrl);
				} else {
					return Result.fail("","不能上传该格式的文件,只能上传mp4、png、jpg、jpeg等常见格式的图片文件");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return Result.csuccess(list);
	}
}
